package com.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ddd.wandemo.R;
import com.util.DensityUtils;

import java.util.ArrayList;
import java.util.List;

public class SelectDayLayout extends FrameLayout {
    private HorizontalScrollView mHsDay;
    private LinearLayout mLlIndicate;
    private Context mContext;
    private List<RelativeLayout> dayViews = new ArrayList<>();
    private List<LinearLayout> dayContents = new ArrayList<>();
    private RelativeLayout selectView;
    private int indicate = 0;
    private onGetDateListener mListener;
    private final String TAG = "SelectDayLayout";

    public SelectDayLayout(Context context) {
        this(context, null);
    }

    public SelectDayLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SelectDayLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View selectView = LayoutInflater.from(context).inflate(R.layout.view_day_select, this, true);
        mLlIndicate = selectView.findViewById(R.id.ll_indicate);
        mHsDay = selectView.findViewById(R.id.hs);

        mHsDay.setOnIndexListener(new HorizontalScrollView.pageIndexListener() {
            @Override
            public void pageIndex(int pageIndex) {
                //获取页码
                changeIndicate(pageIndex);
            }
        });
    }

    public void setViews(List<Integer> data) {
        if (data == null || data.size() == 0) {
            throw new IllegalStateException("no data");
        }
        int contentCount = data.size() / 7;

        for (int i = 0; i < contentCount; i++) {
            LinearLayout ll = new LinearLayout(mContext);
            ViewGroup.LayoutParams params = new MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            mHsDay.addView(ll, params);
            dayContents.add(ll);
        }

        for (int i = 0; i < data.size(); i++) {
            LinearLayout ll = dayContents.get(i / 7);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);

            final RelativeLayout dayItemView = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.item_day_select, null, false);
            dayItemView.setTag(i);
            TextView tvDay = dayItemView.findViewById(R.id.tv);
            tvDay.setText(String.valueOf(data.get(i)));
            dayItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectView.setSelected(false);
                    dayItemView.setSelected(true);
                    selectView = dayItemView;
                    mListener.getDate((Integer) dayItemView.getTag());
                    Log.v(TAG, "page_num" + dayItemView.getTag());
                }
            });

            ll.addView(dayItemView, params);
            dayViews.add(dayItemView);
        }

        for (int i = 0; i < dayViews.size(); i++) {
            if (i == 5) {
                ColorStateList stateColor = getStateColor(mContext, -android.R.attr.state_selected, android.R.attr.state_selected, getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.White));
                ((TextView) dayViews.get(i).findViewById(R.id.tv)).setTextColor(stateColor);
                selectView = dayViews.get(i);
                dayViews.get(i).setSelected(true);
            }
            if (i < 5) {
                ColorStateList stateColor = getStateColor(mContext, -android.R.attr.state_selected, android.R.attr.state_selected, getResources().getColor(R.color.Grey400), getResources().getColor(R.color.White));
                ((TextView) dayViews.get(i).findViewById(R.id.tv)).setTextColor(stateColor);
            }
        }

        initIndicate(contentCount);
    }

    ///接口回调日期index
    public interface onGetDateListener {
        void getDate(int date);
    }

    public void setOnGetDateListener(onGetDateListener listener) {
        this.mListener = listener;
    }

    //初始化指示器
    private void initIndicate(int pageSize) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtils.dp2px(mContext, 5), DensityUtils.dp2px(mContext, 5));
        params.rightMargin = DensityUtils.dp2px(mContext, 8);
        for (int i = 0; i < pageSize; i++) {
            if (i == 1) {
                params.leftMargin = DensityUtils.dp2px(mContext, 8);
            }
//            CircleImageView imageView = new CircleImageView(mContext);
//            imageView.setImageDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.notification_icon_bg_color)));
            ImageView imageView = new ImageView(mContext);
            imageView.setBackground(getResources().getDrawable(R.drawable.bg_indicate_normal));
            mLlIndicate.addView(imageView, params);
        }
        indicateSelect(0);
    }

    private void changeIndicate(int pageIndex) {
        indicateUnSelect(indicate);
        indicate = pageIndex;
        indicateSelect(pageIndex);
    }

    public void indicateSelect(int index) {
//        ((CircleImageView) mLlIndicate.getChildAt(index)).setImageDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.colorAccent)));
        mLlIndicate.getChildAt(index).setBackground(getResources().getDrawable(R.drawable.bg_indicate_selected));
    }

    public void indicateUnSelect(int index) {
//        ((CircleImageView) mLlIndicate.getChildAt(index)).setImageDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.notification_icon_bg_color)));
        mLlIndicate.getChildAt(index).setBackground(getResources().getDrawable(R.drawable.bg_indicate_normal));
    }


    //动态设置selector
    //bg : context.getResources().getDrawable(R.drawable.bg_item_theme)
    private StateListDrawable getStateDrawable(Context context, Drawable selectBg, Drawable normalBg) {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_selected}, selectBg);
        states.addState(new int[]{android.R.attr.state_pressed}, selectBg);
        states.addState(new int[]{android.R.attr.state_checked}, selectBg);
        states.addState(new int[]{}, normalBg);
        return states;
    }

    private ColorStateList getStateColor(Context context, int state1, int state2, int color1, int color2) {
        int[][] states = new int[][]{
                new int[]{state1}, // unchecked  -android.R.attr.state_checked
                new int[]{state2}  // checked android.R.attr.state_checked
        };//把两种状态一次性添加

        int[] colors = new int[]{
                color1,
                color2
        };//把两种颜色一次性添加
        ColorStateList colorStates = new ColorStateList(states, colors);
        return colorStates;
    }
}
